import json
import socket

from google.protobuf import json_format, text_format
from google.protobuf.internal import decoder
from google.protobuf.json_format import MessageToJson, Parse

from protobuf.example_pb2 import Example


def pb_to_json(_bytes):
    return Example().ParseFromString(_bytes)


def json_to_pb(jsonStringResponse):
    bidResponse = Example.BidResponse()
    Parse(jsonStringResponse, bidResponse)
    return bidResponse.SerializeToString()


s = socket.socket()  # 创建 socket 对象
host = "127.0.0.1"
port = 8999
s.bind((host, port))  # 绑定端口

s.listen(5)  # 等待客户端连接
while True:
    c, addr = s.accept()
    print(pb_to_json(c.recv(1024)))
    c.close()
